package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.CrmMemberService;
import com.sun.corba.se.impl.resolver.SplitLocalResolverImpl;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/15
 */
@Component
public class PageData24 {

    @Resource
    private CrmMemberService crmMemberService;

    public String getData(ExcelModel model){

        Page24 page24 = new Page24();

        List<String> dateList1 = DateUtil.getDateList(6);
        List<TempTeam> tempTeams = new ArrayList<>();
        for (String s : dateList1) {
            List<String> transTime = DateUtil.getTransTime(s);
            long teamNewUser = crmMemberService.getTeamNewUser(transTime.get(0), transTime.get(1));
            long teamUser = crmMemberService.getTeamUser(transTime.get(0), transTime.get(1));

            TempTeam tempTeam = new TempTeam();
            tempTeam.setDate(DateUtil.getDateFormat24(transTime.get(0))+"-"+DateUtil.getDateFormat24(transTime.get(1)));
            tempTeam.setAllUsers(teamUser);
            tempTeam.setNewUsers(teamNewUser);
            tempTeams.add(tempTeam);
        }
        page24.setTeamUser(tempTeams);

        //
        List<TempBeen> typeList = new ArrayList<>();
        List<TempBeen> statusList = new ArrayList<>();

        Map<String,List<TempBeen>> typeMap = new HashMap<>();
        Map<String,List<TempBeen>> statusMap = new HashMap<>();

        List<String> dateList = DateUtil.getDateList(2);

        for (String s : dateList) {
            List<String> transTime = DateUtil.getTransTime(s);

            List<TempBeen> typeCount = crmMemberService.getLastSportTypeCount(transTime.get(0), transTime.get(1));
            typeCount.forEach(been -> been.setTempData(s));
            typeList.addAll(typeCount);

            List<TempBeen> tempStatusCount = crmMemberService.getLastSportStatusCount(transTime.get(0), transTime.get(1));
            //补全数据
            List<TempBeen> statusCount = setStatusCount(tempStatusCount);

            statusCount.forEach(been -> been.setTempData(s));
            statusList.addAll(statusCount);
        }
        typeList = getCompletionTypeWeeks(typeList);

        typeList.stream().collect(Collectors.groupingBy(TempBeen::getTempData, toList())).forEach(typeMap::put);
        statusList.stream().collect(Collectors.groupingBy(TempBeen::getTempData, toList())).forEach(statusMap::put);

        List<TempTeam2> typeWeeks = new ArrayList<>();
        List<TempTeam2> statusWeeks = new ArrayList<>();
        typeMap.forEach((k,v) -> {
            TempTeam2 tempTeam2 = new TempTeam2();
            tempTeam2.setBeens(v);
            tempTeam2.setDate(getDate(k));
            typeWeeks.add(tempTeam2);
        });
        statusMap.forEach((k,v)->{
            TempTeam2 tempTeam2 = new TempTeam2();
            tempTeam2.setBeens(v);
            tempTeam2.setDate(getDate(k));
            statusWeeks.add(tempTeam2);
        });



        //按日期排序
        statusWeeks.sort(Comparator.comparing(TempTeam2::getDate));
        typeWeeks.sort(Comparator.comparing(TempTeam2::getDate));

        page24.setTeamStatusCount(statusWeeks);
        page24.setTeamTypeCount(typeWeeks);
        //
        List<String> demandList = DateUtil.getDateList(1);
        List<String> lastWeek = DateUtil.getTransTime(demandList.get(0));
        List<TempBeen> teamDemand = crmMemberService.getTeamDemand(lastWeek.get(0), lastWeek.get(1));
        page24.setTeamDemand(teamDemand);

        //title
        teamDemand.sort(Comparator.comparing(TempBeen::getTempCount).reversed());
        double sum1 = teamDemand.stream().mapToDouble(TempBeen::getTempCount).summaryStatistics().getSum();
        String demandRate = "0";
        String demandName = "";
        if (teamDemand.size() > 0){
            demandRate = StringUtil.decimalFormat2((double)teamDemand.get(0).getTempCount()/sum1 * 100);
            demandName = teamDemand.get(0).getTempName();
        }
        String demandTitle = "周维度数据表现：本周的组队的队伍有'" + demandRate + "%'的队伍是'" + demandName +"'";
        page24.setDemandTitle(demandTitle);

        long teamUser = crmMemberService.getTeamUser(lastWeek.get(0), lastWeek.get(1));
        long teamNewUser = crmMemberService.getTeamNewUser(lastWeek.get(0), lastWeek.get(1));
        String newUserRate = "0";
        if (teamUser != 0 || teamNewUser != 0){
            newUserRate = StringUtil.decimalFormat2((double)teamNewUser/(double)teamUser*100);
        }
        String teamUserTitle = "周维度数据表现：本周有'" + teamUser + "'位用户参与一键组队，新用户占比'" + newUserRate + "'%";
        page24.setTeamUserTitle(teamUserTitle);

        List<TempBeen> typeBeenList = crmMemberService.getLastSportTypeCount(lastWeek.get(0), lastWeek.get(1));
        typeBeenList.sort(Comparator.comparing(TempBeen::getTempCount).reversed());
        String title1 = "";
        if (typeBeenList.size() > 2){
            String tempTitle = typeBeenList.get(0).getTempName() + "和" + typeBeenList.get(1).getTempName();
            title1 = tempTitle+"项目'的组队频次最高";
        }
        String typeTitle = "周维度数据表现：'"+ title1;
        page24.setTypeTitle(typeTitle);

        List<TempBeen> statusCount = crmMemberService.getLastSportStatusCount(lastWeek.get(0), lastWeek.get(1));
        double sum = statusCount.stream().mapToDouble(TempBeen::getTempCount).summaryStatistics().getSum();
        List<TempBeen> tempStatus = statusCount.stream().filter(been -> been.getTempName().equals("组队成功")).collect(toList());
        String statusRate = "0";
        if (tempStatus.size()>0){
            statusRate = StringUtil.decimalFormat2((double)tempStatus.get(0).getTempCount()/sum * 100);
        }
        String statusTitle = "周维度数据表现：本周的组队的队伍中'" + statusRate + "%'用户'组队成功'";
        page24.setStatusTitle(statusTitle);

        page24.setTitle(LogicUtil.getTitle(model,24));
        page24.setDataPeriod(DateUtil.getLastWeekDate());

        return JsonUtil.serialize(page24);
    }

    private List<TempBeen> getCompletionTypeWeeks(List<TempBeen> beens) {

        List<String> dateList = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        for (TempBeen been : beens) {
            if (!dateList.contains(been.getTempData())){
                dateList.add(been.getTempData());
            }
            if (!nameList.contains(been.getTempName())){
                nameList.add(been.getTempName());
            }
        }

        List<TempBeen> complexBeen = new ArrayList<>();
        for (String date : dateList) {
            List<TempBeen> tempBeens = beens.stream().filter(been -> been.getTempData().equals(date)).collect(toList());
            for (String name : nameList) {
                List<TempBeen> tempBeenList = tempBeens.stream().filter(been -> been.getTempName().equals(name)).collect(toList());

                TempBeen been = new TempBeen();
                been.setTempData(date);
                been.setTempName(name);
                complexBeen.add(been);
                if (CollectionUtils.isEmpty(tempBeenList)){
                    been.setTempCount(0);
                }else {
                    been.setTempCount(tempBeenList.get(0).getTempCount());
                }
            }
        }
        return complexBeen;
    }

    private List<TempBeen> setStatusCount(List<TempBeen> tempStatusCount) {
        List<TempBeen> tempBeens = new ArrayList<>();

        List<String> nameList = new ArrayList<>();
        for (TempBeen tempBeen : tempStatusCount) {
            nameList.add(tempBeen.getTempName());
        }
        List<String> list = Arrays.asList("组队失败","组队成功","组队中");
        for (String name : list) {
            if (!nameList.contains(name)){//不包含情况 补充
                TempBeen been = new TempBeen();
                been.setTempName(name);
                been.setTempCount(0);
                tempStatusCount.add(been);
            }
        }
        for (TempBeen tempBeen : tempStatusCount) {
            if (list.contains(tempBeen.getTempName())){
                tempBeens.add(tempBeen);
            }
        }
        return tempBeens;
    }

    //每周一键组队渠道总参与与用户和新用户人数统计
    private TempData getTeamUser(){
        List<String> dateList = DateUtil.getDateList(6);
        TempData tempData = new TempData();
        List<TempTeam> tempTeams = new ArrayList<>();
        for (String s : dateList) {
            List<String> transTime = DateUtil.getTransTime(s);
            long teamNewUser = crmMemberService.getTeamNewUser(transTime.get(0), transTime.get(1));
            long teamUser = crmMemberService.getTeamUser(transTime.get(0), transTime.get(1));

            TempTeam tempTeam = new TempTeam();
            tempTeam.setDate(transTime.get(0)+"-"+transTime.get(1));
            tempTeams.add(tempTeam);
        }

        String newUserRate = StringUtil.decimalFormat2((double)tempTeams.get(tempTeams.size()-1).getNewUsers()/(double) tempTeams.get(tempTeams.size()-1).getAllUsers() * 100);
        String tempStr = "周维度数据表现：本周有'"+tempTeams.get(tempTeams.size()-1).getAllUsers()+"'位用户参与一键组队，新用户占比'"+ newUserRate +"'%";
        tempData.setChildTitle(tempStr);
        tempData.setTempBeens(tempTeams);
        return tempData;
    }

    private String getDate(String date){
        List<String> transTime = DateUtil.getTransTime(date);
        return DateUtil.getDateFormat24(transTime.get(0))+"-"+DateUtil.getDateFormat24(transTime.get(1));
    }

    @Data
    private static class Page24{
        private String title;
        private String teamUserTitle;
        private String typeTitle;
        private String statusTitle;
        private String demandTitle;
        private String dataPeriod;
        private List<TempTeam> teamUser;
        private List<TempTeam2> teamTypeCount;
        private List<TempTeam2> teamStatusCount;
        private List<TempBeen> teamDemand;
    }

    @Data
    private static class TempData{
        private String childTitle;
        private List<TempTeam> tempBeens;
//        private
    }

    @Data
    private static class TempTeam{
        private String date;
        private long allUsers;
        private long newUsers;
    }

    @Data
    private static class TempData2{
        private String childTitle;
        private List<TempTeam2> data;
    }

    @Data
    private static class TempTeam2{
        private String date;
        private List<TempBeen> beens;
    }
}
